<template>
  <a-card :bordered="false">
    <div class="card-title">{{ $route.meta.title }}</div>
    <div class="table-operator">
      <!-- 搜索板块 -->
      <a-row class="row-item-search"  v-if="false">
        <a-form class="search-form" :form="searchForm" layout="inline" @submit="handleSearch">
          <!-- <a-form-item label="轮播图标题">
            <a-input v-decorator="['title']" placeholder="请输入轮播图标题" />
          </a-form-item> -->
          <a-form-item label="状态">
            <a-select v-decorator="['status', { initialValue: -1 }]">
              <a-select-option :value="-1">全部</a-select-option>
              <a-select-option :value="1">启用</a-select-option>
              <a-select-option :value="0">隐藏</a-select-option>
            </a-select>
          </a-form-item>
          <a-form-item class="search-btn">
            <a-button type="primary" icon="search" html-type="submit">搜索</a-button>
          </a-form-item>
        </a-form>
      </a-row>
      <!-- 操作板块 -->
      <div class="row-item-tab clearfix">
        <a-button v-action:add type="primary" icon="plus" @click="handleAdd">新增</a-button>
      </div>
    </div>
    <s-table 
      ref="table"
      :rowKey="(record)=>record.id"
      :loading="isLoading"
      :columns="columns"
      :data="loadData"
      :pageSize="15"
    >
      <!-- 封面图 -->
      <span slot="url" slot-scope="url">
        <a title="点击查看原图" :href="url" target="_blank">
          <img height="50" :src="url" alt="封面图" />
        </a>
      </span>
      <!-- 状态 -->
      <span slot="status" slot-scope="text,item">
        <!-- {{text}} -->
        <a-switch :checked="text== 1 ? true : false" @change="onChangeStatus(item)" />
        <!-- <a-tag :color="text ? 'green' : ''">{{ text ? '显示' : '隐藏' }}</a-tag> -->
      </span>
      <!-- 操作项 -->
      <span slot="action" slot-scope="text, item">
        <a v-action:delete @click="handleDelete(item)">删除</a>
      </span>
    </s-table>
    <!-- <AddForm ref="AddForm" :categoryList="categoryList" @handleSubmit="handleRefresh" />
    <EditForm ref="EditForm" :categoryList="categoryList" @handleSubmit="handleRefresh" /> -->


    <a-modal v-model="visible" :maskClosable="false" title="上传轮播图" @ok="handleOk" @cancel="cancel">
        <uploadFile ref="uloadImg"></uploadFile>
    </a-modal>
  </a-card>


  
</template>

<script>
import * as ArticleApi from '@/api/content/article'
import * as CategoryApi from '@/api/content/article/category'
import { ContentHeader, STable ,} from '@/components'
import uploadFile from '@/components/uploadFile/uploadFile.vue'

import {addSwiper,getSwiper,editSwiper,deleteSwiper} from '@/api/swiperImg'

// import EditForm from './modules/EditForm'
// import AddForm from './modules/AddForm'

// 表格表头
const columns = [
  {
    title: '封面图',
    dataIndex: 'url',
    scopedSlots: { customRender: 'url' }
  },
  {
    title: '状态',
    dataIndex: 'status',
    scopedSlots: { customRender: 'status' }
  },
  {
    title: '操作',
    dataIndex: 'action',
    width: '180px',
    scopedSlots: { customRender: 'action' }
  }
]

export default {
  name: 'Index',
  components: {
    ContentHeader,
    STable,
    uploadFile,
    // AddForm,
    // EditForm
  },
  data () {
    return {
      visible:false,
      expand: false,
      // 当前表单元素
      searchForm: this.$form.createForm(this),
      // 分类列表
      categoryList: [],
      // 查询参数
      queryParam: {},
      // 正在加载
      isLoading: false,
      // 表头
      columns,
      // 加载数据方法 必须为 Promise 对象
      swiperList:[],
      page:'1',
      loadData: param => {
        return getSwiper({page:this.page })
          .then(response => {
            return response.data
          })
      }
    }
  },
  created () {
    
    // 获取分类列表
    this.getCategoryList()
    // this.getSwiperList()
  },
  methods: {
    onChangeStatus(_item){
      console.log(_item);
      let data = {
        id : _item.id,
        status : _item.status==1 ? 2 : 1,
      }
      editSwiper(data).then(res=>{
        console.log(res);
        if (res.status == 200) {
          this.$message.success('修改状态成功')
          this.handleRefresh()
        }
      })
    },
    getSwiperList(){
        getSwiper({page:'1'}).then(res=>{
          console.log(res);
          if (res.code==1) {
            this.swiperList = res.data.data
            
          }
        })
      
    },
    handleOk(){
        if (this.$refs.uloadImg.imageUrl) {
           addSwiper({url:this.$refs.uloadImg.imageUrl}).then(res=>{
              console.log(res);
              this.$message.success('上传成功')
              this.$refs.uloadImg.imageUrl = ''
              this.visible = false
              this.handleRefresh(true)
           })
        }else{
          this.$message.error('请先上传图片')
        }
        
    },
    cancel(){
      this.$refs.uloadImg.imageUrl = ''
    },

    /**
     * 获取分类列表
     */
    getCategoryList () {
      this.isLoading = true
      CategoryApi.list()
        .then(result => {
          this.categoryList = result.data.list
        })
        .finally(result => {
          this.isLoading = false
        })
    },

    // 确认搜索
    handleSearch (e) {
      e.preventDefault()
      this.searchForm.validateFields((error, values) => {
        if (!error) {
          this.queryParam = { ...this.queryParam, ...values }
          this.handleRefresh(true)
        }
      })
    },

    /**
     * 删除记录
     */
    handleDelete (item) {
      const app = this
      const modal = this.$confirm({
        title: '您确定要删除该记录吗?',
        content: '删除后不可恢复',
        onOk () {
          return deleteSwiper({ id: item.id })
            .then(res => {
              app.$message.success('删除成功', 1.5)
              app.handleRefresh()
            })
            .finally(result => {
              modal.destroy()
            })
        }
      })
    },

    /**
     * 新增记录
     */
    handleAdd () {
      // this.$refs.AddForm.add()
      this.visible = true
    },

    /**
     * 编辑记录
     */
    handleEdit (item) {
      // this.$refs.EditForm.edit(item.article_id)
    },

    /**
     * 刷新列表
     * @param Boolean bool 强制刷新到第一页
     */
    handleRefresh (bool = false) {
      this.$refs.table.refresh(bool)
    }

  }
}
</script>
<style lang="less" scoped>
.ant-card-body {
  padding: 22px 29px 25px;
}
// 筛选tab
.tab-list {
  margin-right: 20px;
}
</style>
